package com.yizisu.basemvvm.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.isInvisible
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.yizisu.basemvvm.R
import com.yizisu.basemvvm.utils.invisible
import com.yizisu.basemvvm.utils.isInVisible
import com.yizisu.basemvvm.utils.isVisible
import com.yizisu.basemvvm.utils.visible
import com.yizisu.basemvvm.widget.BaseFrameLayout

open class SimpleSwitchView : SwipeRefreshLayout {
    private var _contentView: View? = null
    private var _loadingView: View? = null
    private var _otherView: View? = null
    //是否拦截子view
    var isEnableChildView = true

    val contentView: View?
        get() = _contentView
    val loadingView: View?
        get() = _loadingView
    val otherView: View?
        get() = _otherView
    //是否可以下拉刷新
    var isCanSwipeRefresh = false
        set(value) {
            field = value
            isEnabled = value
        }
    private val contentParentView by lazy { BaseFrameLayout(context) }


    constructor(context: Context, getContentView: Function1<ViewGroup, View>) : super(context) {
        _contentView = getContentView.invoke(contentParentView)
        initSwitchView()
    }

    //xml中创建
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        context.apply {
            val typedArray = obtainStyledAttributes(attrs, R.styleable.SimpleSwitchView)
            //显示内容的布局
            val contentLayoutRes =
                typedArray.getResourceId(R.styleable.SimpleSwitchView_contentLayout, View.NO_ID)
            if (contentLayoutRes != View.NO_ID) {
                _contentView =
                    LayoutInflater.from(context).inflate(contentLayoutRes, contentParentView, false)
            }
            //loading布局
            val loadingLayoutRes =
                typedArray.getResourceId(R.styleable.SimpleSwitchView_loadingLayout, View.NO_ID)
            if (loadingLayoutRes != View.NO_ID) {
                _loadingView =
                    LayoutInflater.from(context).inflate(loadingLayoutRes, contentParentView, false)
            }
            //其他布局
            val otherLayoutRes =
                typedArray.getResourceId(R.styleable.SimpleSwitchView_otherLayout, View.NO_ID)
            if (otherLayoutRes != View.NO_ID) {
                _otherView =
                    LayoutInflater.from(context).inflate(otherLayoutRes, contentParentView, false)
            }
            isCanSwipeRefresh =
                typedArray.getBoolean(R.styleable.SimpleSwitchView_canSwipeRefresh, false)
            typedArray.recycle()
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        //默认会有一个旋转的imageView
        when (childCount) {
            1 -> {
                //和在代码中创建一样，不作处理
                initSwitchView()
            }
            2 -> {
                val view = getChildAt(1)
                if (view != contentParentView) {
                    _contentView = view
                    //移除这个view，然后添加到viewSwtich
                    removeView(view)
                    initSwitchView()
                }
            }
            else -> {
                throw IllegalArgumentException("最多只能有1个子view")
            }
        }
    }

    //添加子view
    private var isInitView = false

    private fun initSwitchView() {
        if (isInitView) {
            return
        }
        isInitView = true
        isEnabled = isCanSwipeRefresh
        addView(contentParentView, getChildLayoutParams())
        //添加默认的otherView
        val other = otherView ?: getDefaultOtherView(context)
        _otherView = other
        contentParentView.addView(other, other.layoutParams ?: getChildLayoutParams())
        //添加content
        val content = contentView
        if (content != null) {
            contentParentView.addView(content)
        }
        //添加默认loading
        val loading = loadingView ?: getDefaultLoadingView(context)
        _loadingView = loading
        contentParentView.addView(loading, loading.layoutParams ?: getChildLayoutParams())
        showContentView()
    }

    /**
     * 默认充满父布局
     */
    private fun getChildLayoutParams(): FrameLayout.LayoutParams {
        return FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
    }

    //默认加载数据的view
    protected open fun getDefaultLoadingView(context: Context): View {
        return LayoutInflater.from(context)
            .inflate(R.layout.default_loading_view, contentParentView, false)
    }

    //默认显示其他view
    protected open fun getDefaultOtherView(context: Context): View {
        return LayoutInflater.from(context)
            .inflate(R.layout.default_empty_view, contentParentView, false)
    }

    /**
     * 显示包含的布局view
     */
    fun showContentView() {
        isEnableChildView = true
        hideIfShow(otherView)
        hideIfShow(loadingView)
        showIfHide(contentView)
    }

    /**
     * 可以重写这个方法设置动画效果
     */
    open fun hideIfShow(view: View?) {
        view.invisible()
//        val animEnd = -view.width.toFloat()
//        if (view.translationX != animEnd) {
//            view.animate().cancel()
//            view.animate().setDuration(1000)
//                    .translationX(animEnd)
//                    .start()
//        }
    }

    /**
     * 可以重写这个方法设置动画效果
     */
    open fun showIfHide(view: View?) {
        view.visible()
//        val animEnd =0f
//        if (view.translationX != animEnd) {
//            view.animate().cancel()
//            view.animate().setDuration(1000)
//                    .translationX(animEnd)
//                    .start()
//        }
    }

    /**
     * 显示加载view
     */
    fun showLoadingView(isHideContentView: Boolean = false, isEnableContentView: Boolean = false) {
        isEnableChildView = isEnableContentView
        hideIfShow(otherView)
        if (isHideContentView) {
            hideIfShow(contentView)
        } else {
            showIfHide(contentView)
        }
        showIfHide(loadingView)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        if (!isEnableChildView) {
            return true
        }
        return super.onInterceptTouchEvent(ev)
    }

    /**
     * 显示其他view
     */
    fun showOtherView() {
        isEnableChildView = true
        hideIfShow(contentView)
        hideIfShow(loadingView)
        showIfHide(otherView)
    }

    fun startRefresh() {
        isRefreshing = true
    }

    fun stopRefresh() {
        isRefreshing = false
    }
}